package kadledav.adventure.framework.controll.commands;

import java.io.PrintStream;
import java.util.List;
import java.util.Scanner;

import kadledav.adventure.framework.controll.ControllConstants;
import kadledav.adventure.framework.controll.commands.abstr.ParameterActionCommand;
import kadledav.adventure.framework.model.world.Passage;
import kadledav.adventure.framework.model.world.Player;

public class GoCommand extends ParameterActionCommand {

	public GoCommand(PrintStream out, Scanner in, Player player) {
		super(out, in, player);
	}

	@Override
	public void execute(String[] userCommand) {
		String commandPrefix = ControllConstants.PASSAGE_PREFIX;
		Integer passageNumber = parseFirstNumber(userCommand, commandPrefix);
		List<Passage> possibleExits = player.getCurrentLocation().getPossibleExits();
		if( passageNumber == null || !checkIsInRange(possibleExits, passageNumber)){
			wrongCommandArgument();
			return;
		}
		Passage passage = possibleExits.get(passageNumber);
		if( passage == null){
			wrongCommandArgument();
			return;
		}
		doTheGoAction(passage);
	}

	private void doTheGoAction(Passage passage) {
		boolean passThePassage = player.goThroughPassage(passage);
		if(passThePassage){
			out.println("Prosel jsem. Moje soucastna pozice je "+player.getCurrentLocation().getName());
		}else{
			out.println("Tudy nelze projit.");
		}
		out.println();
	}
}
